home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Textures SDK / common / trilib.c < prev    next >
C/C++ Source or Header  |  1998-12-02  |  4KB  |  181 lines

  1. /***
  2. *
  3. *    Copyright (c) 1998, Valve LLC. All rights reserved.
  4. *    
  5. *    This product contains software technology licensed from Id 
  6. *    Software, Inc. ("Id Technology").  Id Technology (c) 1996 Id Software, Inc. 
  7. *    All Rights Reserved.
  8. *
  9. ****/
  10.  
  11. //
  12. // trilib.c: library for loading triangles from an Alias triangle file
  13. //
  14.  
  15. #include <stdio.h>
  16. #include "cmdlib.h"
  17. #include "mathlib.h"
  18. #include "trilib.h"
  19.  
  20. // on disk representation of a face
  21.  
  22.  
  23. #define    FLOAT_START    99999.0
  24. #define    FLOAT_END    -FLOAT_START
  25. #define MAGIC       123322
  26.  
  27. //#define NOISY 1
  28.  
  29. typedef struct {
  30.     float v[3];
  31. } vector;
  32.  
  33. typedef struct
  34. {
  35.     vector n;    /* normal */
  36.     vector p;    /* point */
  37.     vector c;    /* color */
  38.     float  u;    /* u */
  39.     float  v;    /* v */
  40. } aliaspoint_t;
  41.  
  42. typedef struct {
  43.     aliaspoint_t    pt[3];
  44. } tf_triangle;
  45.  
  46.  
  47. void ByteSwapTri (tf_triangle *tri)
  48. {
  49.     int        i;
  50.     
  51.     for (i=0 ; i<sizeof(tf_triangle)/4 ; i++)
  52.     {
  53.         ((int *)tri)[i] = BigLong (((int *)tri)[i]);
  54.     }
  55. }
  56.  
  57. void LoadTriangleList (char *filename, triangle_t **pptri, int *numtriangles)
  58. {
  59.     FILE        *input;
  60.     float       start;
  61.     char        name[256], tex[256];
  62.     int         i, count, magic;
  63.     tf_triangle    tri;
  64.     triangle_t    *ptri;
  65.     int            iLevel;
  66.     int            exitpattern;
  67.     float        t;
  68.  
  69.  
  70.     t = -FLOAT_START;
  71.     *((unsigned char *)&exitpattern + 0) = *((unsigned char *)&t + 3);
  72.     *((unsigned char *)&exitpattern + 1) = *((unsigned char *)&t + 2);
  73.     *((unsigned char *)&exitpattern + 2) = *((unsigned char *)&t + 1);
  74.     *((unsigned char *)&exitpattern + 3) = *((unsigned char *)&t + 0);
  75.  
  76.     if ((input = fopen(filename, "rb")) == 0) {
  77.         fprintf(stderr,"reader: could not open file '%s'\n", filename);
  78.         exit(0);
  79.     }
  80.  
  81.     iLevel = 0;
  82.  
  83.     fread(&magic, sizeof(int), 1, input);
  84.     if (BigLong(magic) != MAGIC) {
  85.         fprintf(stderr,"File is not a Alias object separated triangle file, magic number is wrong.\n");
  86.         exit(0);
  87.     }
  88.  
  89.     ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));
  90.  
  91.     *pptri = ptri;
  92.  
  93.     while (feof(input) == 0) {
  94.         fread(&start,  sizeof(float), 1, input);
  95.         *(int *)&start = BigLong(*(int *)&start);
  96.         if (*(int *)&start != exitpattern)
  97.         {
  98.             if (start == FLOAT_START) {
  99.                 /* Start of an object or group of objects. */
  100.                 i = -1;
  101.                 do {
  102.                     /* There are probably better ways to read a string from */
  103.                     /* a file, but this does allow you to do error checking */
  104.                     /* (which I'm not doing) on a per character basis.      */
  105.                     ++i;
  106.                     fread( &(name[i]), sizeof( char ), 1, input);
  107.                 } while( name[i] != '\0' );
  108.     
  109.     //            indent();
  110.     //            fprintf(stdout,"OBJECT START: %s\n",name);
  111.                 fread( &count, sizeof(int), 1, input);
  112.                 count = BigLong(count);
  113.                 ++iLevel;
  114.                 if (count != 0) {
  115.     //                indent();
  116.     
  117.     //                fprintf(stdout,"NUMBER OF TRIANGLES: %d\n",count);
  118.     
  119.                     i = -1;
  120.                     do {
  121.                         ++i;
  122.                         fread( &(tex[i]), sizeof( char ), 1, input);
  123.                     } while( tex[i] != '\0' );
  124.     
  125.     //                indent();
  126.     //                fprintf(stdout,"  Object texture name: '%s'\n",tex);
  127.                 }
  128.     
  129.                 /* Else (count == 0) this is the start of a group, and */
  130.                 /* no texture name is present. */
  131.             }
  132.             else if (start == FLOAT_END) {
  133.                 /* End of an object or group. Yes, the name should be */
  134.                 /* obvious from context, but it is in here just to be */
  135.                 /* safe and to provide a little extra information for */
  136.                 /* those who do not wish to write a recursive reader. */
  137.                 /* Mia culpa. */
  138.                 --iLevel;
  139.                 i = -1;
  140.                 do {
  141.                     ++i;
  142.                     fread( &(name[i]), sizeof( char ), 1, input);
  143.                 } while( name[i] != '\0' );
  144.     
  145.     //            indent();
  146.     //            fprintf(stdout,"OBJECT END: %s\n",name);
  147.                 continue;
  148.             }
  149.         }
  150.  
  151. //
  152. // read the triangles
  153. //        
  154.         for (i = 0; i < count; ++i) {
  155.             int        j;
  156.  
  157.             fread( &tri, sizeof(tf_triangle), 1, input );
  158.             ByteSwapTri (&tri);
  159.             for (j=0 ; j<3 ; j++)
  160.             {
  161.                 int        k;
  162.  
  163.                 for (k=0 ; k<3 ; k++)
  164.                 {
  165.                     ptri->verts[j][k] = tri.pt[j].p.v[k];
  166.                 }
  167.             }
  168.  
  169.             ptri++;
  170.  
  171.             if ((ptri - *pptri) >= MAXTRIANGLES)
  172.                 Error ("Error: too many triangles; increase MAXTRIANGLES\n");
  173.         }
  174.     }
  175.  
  176.     *numtriangles = ptri - *pptri;
  177.  
  178.     fclose (input);
  179. }
  180.  
  181.